#include "ResMgr.h"
#include "Texture.h"
#include "Utils.h"
#include "Const.h"

ResMgr* ResMgr::instance = 0;

void ResMgr::AddTexture(Texture* tex)
{
	textures[tex->GetID()] = tex;
}

ResMgr::ResMgr()
{
	textures = new Texture*[SIZE_OF_TEXTURE_ARRAY];
	//memset(textures, 0, sizeof(int)*SIZE_OF_TEXTURE_ARRAY);
	for(int i = 0; i < SIZE_OF_TEXTURE_ARRAY; i++)
	{
		textures[i] = 0;
	}
}

Texture* ResMgr::GetTexture(int id)
{
	return textures[id];
}

void ResMgr::RemoveTexture(int id)
{
	Texture * tex = textures[id];
	textures[id] = 0;
	DELETE_SAFELY(tex);
}

ResMgr* ResMgr::GetInstance()
{
	if(instance == 0)
	{
		PRINT("Init ResMgr\n");
		instance = new ResMgr();
	}
	return instance;
}

ResMgr::~ResMgr()
{
	Texture* tex;
	for(int i = 0; i< SIZE_OF_TEXTURE_ARRAY; i++)
	{
		tex = textures[i];
		DELETE_SAFELY(tex);
	}
	DELETE_SAFELY_ARRAY(textures);
}

void ResMgr::Destroy()
{
	PRINT("Destroy ResMgr\n");
	DELETE_SAFELY(instance);
}

void ResMgr::LoadIntro()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"intro-back.png", TID_INTRO_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadIntro()
{
	RemoveTexture(TID_INTRO_BACK);
}

void ResMgr::LoadMainMenu()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"menu-button.png", TID_MENU_BUTTON);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"main-menu-back.png", TID_MAIN_MENU_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadMainMenu()
{
	RemoveTexture(TID_MENU_BUTTON);
	RemoveTexture(TID_MAIN_MENU_BACK);
}

void ResMgr::LoadChooseIslandRes()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"islands.png", TID_ISLANDS_BUTTON);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"main-menu-back.png", TID_MAIN_MENU_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadChooseIslandRes()
{
	RemoveTexture(TID_ISLANDS_BUTTON);
	RemoveTexture(TID_MAIN_MENU_BACK);
}

void ResMgr::LoadChooseLevelRes()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"levels.png", TID_LEVELS_BUTTON);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"main-menu-back.png", TID_MAIN_MENU_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadChooseLevelRes()
{
	RemoveTexture(TID_ISLANDS_BUTTON);
	RemoveTexture(TID_MAIN_MENU_BACK);
}

void ResMgr::LoadFruitBox()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"country-back.png", TID_COUNTRY_BACK);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"lock.png", TID_LOCK);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"fruit.png", TID_FRUIT);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"exploding.png", TID_EXPLODING);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"number.png", TID_NUMBER);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"button-in-game.png", TID_BUTTON_IN_GAME);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"option-back.png", TID_OPTION_BACK);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"effect-special-diamond.png", TID_EFFECT_SPECIAL_DIA);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"head-back.png", TID_HEAD_BACK);
	AddTexture(texture);
	texture = new Texture(ROOT_IMG_DIR"empty-cell.png", TID_EMPTY_CELL);
	AddTexture(texture);
}

void ResMgr::UnloadFruitBox()
{
	RemoveTexture(TID_COUNTRY_BACK);
	RemoveTexture(TID_LOCK);
	RemoveTexture(TID_FRUIT);
	RemoveTexture(TID_EXPLODING);
	RemoveTexture(TID_NUMBER);
	RemoveTexture(TID_BUTTON_IN_GAME);
	RemoveTexture(TID_OPTION_BACK);
}

void ResMgr::LoadWin()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"win-back.png", TID_WIN_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadWin()
{
	RemoveTexture(TID_WIN_BACK);
}

void ResMgr::LoadGameOver()
{
	Texture* texture;
	texture = new Texture(ROOT_IMG_DIR"gameover-back.png", TID_GAMEOVER_BACK);
	AddTexture(texture);
}

void ResMgr::UnloadGameOver()
{
	RemoveTexture(TID_GAMEOVER_BACK);
}



void ResMgr::UnloadAllTexture()
{
	Texture* tex;
	for(int i = 0; i< SIZE_OF_TEXTURE_ARRAY; i++)
	{
		tex = textures[i];
		textures[i] = 0;
		DELETE_SAFELY(tex);
	}
}